﻿/*
* @author Mikko Miettinen
*/
//raycast hit handle
var hit : RaycastHit;
//GUI text handles
var timeGUI : GUIText;
var scoreGUI : GUIText;
// handlers to picked cards
var matchOne : GameObject;
var matchTwo : GameObject;

// basic game vars.
var cardsLeft = 18;
var timeLeft = 0;
var failedPairs = 0;
var score :int = 0;
var scoreMultiplier =6 ;

function Start()
{
	//initialize your scores and time
	var score :int = 0;
	var timeLeft = 0;
	
}


while (true) 
{
if (Input.GetKeyDown(KeyCode.Escape)) { Application.LoadLevel("Menu"); }
	// Use Raycast to pick cards that have mesh colliders attached...
	// and for making possible to debug on unity, as game will be on
	// touchscreen device
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	
	if (Input.GetMouseButtonDown (0))
	{
		if (Physics.Raycast (ray, hit, 100)) 
		{
			//if matchOne hasn't been assigned yet, then do this
			if (! matchOne)
			{
				yield new WaitForSeconds(0.1);
				revealCardOne();
			}
			else//matchOne is assigned, do this
			{
			// raycast hit is not hitting matchOne card 
			// and this matchTwo is still unassigned
			if(matchOne != hit.transform.gameObject && matchTwo == null){
				revealCardTwo();
				yield new WaitForSeconds (0.3); 
				}
			}
		}
	}
	//update the game clock
	timeLeft =  Time.timeSinceLevelLoad;
	timeGUI.text = "" + timeLeft;
	
	//game ends when there are no card pairs left(aka no cards left)
	if (cardsLeft == 0) 
	{
	yield new WaitForSeconds (0.5); 
		yield gameWon();
	}
	
	// Wait for next frame
	yield;
}
	//picking your first card
function revealCardOne() 
{	
	//assing "touched" card to matchOne
	matchOne = hit.transform.gameObject;
	//turns the card
	iTween.RotateAdd(matchOne,iTween.Hash("time",0.5f,"z",180));
	
	// Wait for the animation to have finished
	yield WaitForSeconds (0.2);
}

 	//function for second card and major game logic for what happens 
 	//if they are valid pair
function revealCardTwo()
{	

	//assing "touched" card to matchTwo
	matchTwo = hit.transform.gameObject;
	
	//if player tries to pick same card, nothing happens and 
	//is basically ignored and nothing is done
	if (matchOne == matchTwo) 
	{
		return;
	}
	//turns the card
	iTween.RotateAdd(matchTwo,iTween.Hash("time",0.5f,"z",180));
	
	yield new WaitForSeconds (0.2);
	
	// Wait for the animation to have finished
	
	yield WaitForSeconds (0.2);//hit.transform.gameObject.animation.clip.length);
	
	if (matchOne.name == matchTwo.name) //was a pair valid pair remove them from board and credit player
	{
		yield new WaitForSeconds (1.0);
		
		cardsLeft -= 2;
		//calculating the score, could be fancier but will do
		var tempSc = score; 
		score += ((1 * (( 25 - 0)/25))*( 1880/(timeLeft +1) ) );
		//iTween animating score running up with it's ValueTo() function
		iTween.ValueTo(gameObject,{"from":tempSc, "to":score,"time":.6f,"onUpdate":"UpdateScoreDisplay"});

		//update score to game manager
		GM.LastScore = score;
		
		yield new WaitForSeconds (0.5);
		Destroy (matchOne);
		Destroy (matchTwo);
		matchOne = null;
		matchTwo = null; 

	}
	
	else	//was not valid pair,turn the cards facedown
	{
		yield new WaitForSeconds (0.3); 
		
		iTween.RotateAdd(matchOne,iTween.Hash("time",0.2f,"z",180));
		
		iTween.RotateAdd(matchTwo,iTween.Hash("time",0.2f,"z",180));
			matchOne = null;
			matchTwo = null; 
	}
	
	
	
}
//function for making valueTo iTween function to work 
//at animating the score numbers
function UpdateScoreDisplay(newScore:int){
	scoreGUI.text =""+ newScore.ToString();
}

function gameWon ()
{
	Application.LoadLevel("GameOver");

	yield new WaitForSeconds (3);
	Destroy (this);
}
